﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using BLToolkit.Data;
using BLToolkit.Data.Linq;

namespace DAL
{

    public class NhaCungCapRepository : INhaCungCapRepository

    {
        
        private readonly DbManager db;
        public NhaCungCapRepository()
        { db = new DbManager("QuanLyBilliards"); }


        public NhaCungCap GetByMaNhaCungCap(string ma)
        {
            return db.GetTable<NhaCungCap>().SingleOrDefault(tk => tk.MaNCC == ma);
        }

        public NhaCungCap GetByTenNhaCungCap(string ma)
        {
            return db.GetTable<NhaCungCap>().SingleOrDefault(tk => tk.TenNCC == ma);
        }

        public IQueryable<NhaCungCap> GetAll()
        {
            return db.GetTable<NhaCungCap>();
        }

        public bool AddNew(NhaCungCap nhaCC)
        {
            return db.Insert(nhaCC) > 0;
        }

        public bool Update(NhaCungCap nhaCC)
        {
            return db.Update(nhaCC) > 0;
        }

        public bool Delete(NhaCungCap nhaCC)
        {
            return db.Delete(nhaCC) > 0;
        }



        public NhaCungCap getByIdDetail(string maNCC)
        {
            NhaCungCap ncc = GetByMaNhaCungCap(maNCC);
            if (ncc != null)

                ncc.dshanghoa = db.GetTable<HangHoa>().Where(n => n.NhaCC == maNCC).ToList();
            return ncc;
        }


        public List<NhaCungCap> TimTheoSoDT(string sodt)
        {
            return db.GetTable<NhaCungCap>().Where(n => n.SDT == sodt).ToList();
        }

        public List<HangHoa> TimTheoHHCC(string hhcc)
        {
            return db.GetTable<HangHoa>().Where(n => n.TenHH == hhcc).ToList();
            

            return db.GetTable<HangHoa>().Where(n => n.TenHH == hhcc).ToList();

        }

        public List<NhaCungCap> TimTheoTenNCC(string tenNcc)
        {
            return db.GetTable<NhaCungCap>().Where(n => n.TenNCC.Contains(tenNcc)).ToList();
        }
        List<NhaCungCap> INhaCungCapRepository.TimTheoHHCC(string hhcc)
        {
            return db.GetTable<NhaCungCap>().ToList();
        }




        public NhaCungCap GetByIdNhaCungCap(string p)
        {
            return db.GetTable<NhaCungCap>().SingleOrDefault(tk => tk.TenNCC ==p);
        }
    }
}
